package com.xinqi.modules.prepare.question.controller.web;

import com.xinqi.common.base.dto.QuestionBaseDTO;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.prepare.paper.dto.req.QuestionTypeNumDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionBankCopyDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionBankCreateDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionBankEditScoreDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionBankQueryDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionBankSortDTO;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionBankUpdateDTO;
import com.xinqi.modules.prepare.question.dto.rsp.PrepareQuestionBankResultDTO;
import com.xinqi.modules.prepare.question.dto.rsp.PrepareQuestionStatsDataDTO;
import com.xinqi.modules.prepare.question.service.PrepareQuestionBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 备课区测评卷试题表APP控制层
 *
 * @author: system
 * @date: 2023/03/19
 */
@Tag(name = "Web-备课区-试题库", description = "备课区-试题库Web接口")
@RestController
@RequestMapping("/web/prepare")
@RequiredArgsConstructor
public class PrepareQuestionBankWebController {
    private final PrepareQuestionBankService prepareQuestionBankService;


    /**
     * 备课区测评卷试题表-分页
     */
    @Operation(summary = "备课区测评卷试题表-分页")
    @GetMapping("/{prepare-id}/question-bank/page")
    public R<PageInfo<PrepareQuestionBankResultDTO>> page(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                          @ParameterObject PrepareQuestionBankQueryDTO dto) {
        dto.setPrepareId(prepareId);
        PageInfo<PrepareQuestionBankResultDTO> page = prepareQuestionBankService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 备课区测评卷试题表-详情
     */
    @Operation(summary = "备课区测评卷试题表-详情")
    @GetMapping("/{prepare-id}/question-bank/{id}")
    public R<PrepareQuestionBankResultDTO> findById(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                    @Parameter(description = "试题ID", example = "100001") @PathVariable("id") Long id) {
        PrepareQuestionBankResultDTO dto = prepareQuestionBankService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增备课区测评卷试题表
     */
    @Operation(summary = "新增备课区试题表")
    @PostMapping("/{prepare-id}/question-bank")
    public R<Long> insert(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                          @RequestBody PrepareQuestionBankCreateDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionBankService.insert(dto));
    }

    /**
     * 新增备课区测评卷试题表
     */
    @Operation(summary = "批量新增备课区试题表")
    @PostMapping("/{prepare-id}/question-bank/batch-add")
    public R<Boolean> insertBatch(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                  @RequestBody List<PrepareQuestionBankCreateDTO> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(v -> v.setPrepareId(prepareId));
        }
        return R.data(prepareQuestionBankService.insertBatch(prepareId, list));
    }

    /**
     * 查询试题汇总信息
     */
    @Operation(summary = "查询试题汇总信息")
    @GetMapping("/{prepare-id}/question-bank/stats")
    public R<PrepareQuestionStatsDataDTO> questionStats(@Parameter(description = "备课区ID", example = "100001") @PathVariable("prepare-id") Long prepareId) {
        return R.data(prepareQuestionBankService.questionStats(prepareId));
    }

    /**
     * 修改备课区测评卷试题表
     */
    @Operation(summary = "修改备课区测评卷试题表")
    @PutMapping("/{prepare-id}/question-bank/{id}")
    public R<Boolean> update(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                             @Parameter(description = "试题ID", example = "100001") @PathVariable("id") Long id,
                             @RequestBody PrepareQuestionBankUpdateDTO dto) {
        dto.setId(id);
        return R.data(prepareQuestionBankService.updateById(dto));
    }

    /**
     * 删除备课区测评卷试题表
     */
    @Operation(summary = "删除备课区试题")
    @DeleteMapping("/{prepare-id}/question-bank/{id}")
    public R<Boolean> delete(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                             @Parameter(description = "试题ID", example = "100001") @PathVariable("id") Long id) {
        return R.data(prepareQuestionBankService.deleteById(id));
    }

    /**
     * 批量删除试题
     */
    @Operation(summary = "批量删除试题")
    @DeleteMapping("/{prepare-id}/question-bank/batch")
    public R<Boolean> deleteBatch(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                  @RequestBody List<Long> ids) {
        return R.data(prepareQuestionBankService.deleteBatch(prepareId, ids));
    }

    /**
     * 批量改分值
     */
    @Operation(summary = "试题批量改分值")
    @PutMapping("/{prepare-id}/question-bank/edit-score")
    public R<Boolean> editQuestionScore(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                        @RequestBody PrepareQuestionBankEditScoreDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionBankService.editQuestionScore(dto));
    }

    /**
     * 批量改分值
     */
    @Operation(summary = "试题批量排序")
    @PutMapping("/{prepare-id}/question-bank/sort")
    public R<Boolean> sortQuestionBank(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                       @RequestBody PrepareQuestionBankSortDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionBankService.sortQuestionBank(dto));
    }

    /**
     * 批量复制试题
     */
    @Operation(summary = "批量复制试题")
    @PutMapping("/{prepare-id}/question-bank/copy")
    public R<Boolean> copy(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                           @RequestBody PrepareQuestionBankCopyDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareQuestionBankService.copy(dto));
    }

    /**
     * 随机获取试题
     */
    @Operation(summary = "随机获取试题")
    @PostMapping("/{prepare-id}/question-bank/random")
    public R<List<QuestionBaseDTO>> random(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                           @Parameter(description = "试题数量列表", example = "") @RequestBody List<QuestionTypeNumDTO> dto) {
        return R.data(prepareQuestionBankService.random(prepareId, dto));
    }
}
